var t = new(require("../../../utils/http.js").HTTP)();
let {
  addFastPrograms
} = require("../../../api/programs/add");
let {
  intention,
  getsjxq,
  linkmans,
  getFllowDetail
} = require("../../../api/attract/select");

Page({
  data: {
    xxbtshow:false,
    dkShow: false,
    xqList: [],
    // 客户
    khly: [],
    khyxdj: [],
    infor: [],
    show: !1,
    collected: !0,
    zanwu: !0,
    attract: ["基本信息", "商机需求", "联系信息", "跟进记录"],
    investment: 0,

    // 提报流转
    Info: wx.getStorageSync("userInfoMy"),
    GocreatedShow: false,
    programType: ["载体项目", "产业项目", "科技项目", "人才项目", "政策项目"],
    systemInfo: null,
    createdShow: false,
    createdFormProject: false,
    pickerAddShow: false,
    systemInfo: {},
  },
  xxbtClick(){
    this.setData({
      xxbtshow:!this.data.xxbtshow
    })
  },
  goLookDk(e) {
   let {type}=e.currentTarget.dataset
    let infor=wx.getStorageSync('infor')
    switch (type) {
    
      case 'bk':
        let gxsj = this.formatTime(this.data.follow.gjsj, 2)
        console.log(this.data.follow.bid,gxsj)
        wx.navigateTo({
          url: `/pages/intention/follow_dk/follow?khbh=${this.data.follow.khbh}bid=${this.data.follow.bid}&affiliation=inner&tjsj=${gxsj}`,
        })
        break;
      default:
        break;
    }
  },
  lxrshowC(e){
    let {index,booll}=e.currentTarget.dataset
    let IndexB=`linkman[${index}].BooleanLxr`
    // console.log(index,IndexB,booll)
    this.setData({
      [IndexB]:!booll
    })
  },
  opendak() {
    this.setData({
      dkShow: !this.data.dkShow
    })
  },
  goxq(e) {
    let {
      qxid
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../kh_yyxq/kh_yyxq?qymc=${this.data.infor.qymc}&yxxm=${this.data.infor.yxxm}&qxid=${qxid}`,
    });
  },
  goLook(e) {
    let {
      type,
      lid
    } = e.currentTarget.dataset;
    console.log(lid, type, e);
    if (type == 1) {
      wx.navigateTo({
        url: `../contact/contact?IID=${lid}`,
      });
    } else {
      wx.navigateTo({
        url: "../compile/compile?lID=" + lid,
      });
    }
  },
  abandonTosat() {
    wx.showToast({
      title: "请至数据平台进行放弃",
      icon: "none",
    });
  },
  // 提报流转

  onPush() {
    try {
      this.data.systemInfo.xmmc === "" || this.data.systemInfo.xmsq === "" ?
        wx.showToast({
          title: "请填写必填项",
          icon: "error",
          duration: 2000, //持续的时间
        }) :
        this.Add();
    } catch (error) {
      console.log(error);
    }
  },

  Add() {
    let time = new Date().getTime();
    //将时间戳格式转换成年月日时分秒
    let date = new Date(time);
    let xmztsja = 24 * 60 * 60 * 1000 * 15;
    let JZtime = new Date(time + xmztsja);
    let jz_time = `${JZtime.getFullYear()}-${
      JZtime.getMonth() + 1 < 10
        ? "0" + (JZtime.getMonth() + 1)
        : JZtime.getMonth() + 1
    }-${JZtime.getDate() < 10 ? "0" + JZtime.getDate() : JZtime.getDate()}`;
    let bbTime = `${date.getFullYear()}-${
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1
    }-${date.getDate() < 10 ? "0" + date.getDate() : date.getDate()}`;
    let tjTime = `${
      date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
    }:${date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()}:${
      date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
    }`;
    this.setData({
      "systemInfo.bbsj": bbTime,
      "systemInfo.tjsj": `${bbTime} ${tjTime}`,
      "systemInfo.xmztsja": `${jz_time} ${tjTime}`,
      "systemInfo.tkjzsj": `${jz_time} ${tjTime}`,
      "systemInfo.khbh": this.data.infor.khbh,
      "systemInfo.qymc": this.data.infor.qymc,
      "systemInfo.xmly": "招商意向客户",
    });
    console.log(this.data.systemInfo);
    addFastPrograms(this.data.systemInfo)
      .then((res) => {
        wx.showToast({
          title: "新增成功，请填写补充资料",
          icon: "none",
        });
        setTimeout(() => {
          this.setData({
            showDialog: true
          });
        }, 1000);
      })
      .then(() => {
        addOperateLog({
          type: 1,
          khbh: "暂无",
          xmbh: this.data.systemInfo.xmbh,
          xmmc: this.data.systemInfo.xmmc,
          bm: this.data.systemInfo.bm,
          tname: this.data.systemInfo.tname,
          tjsj: this.data.systemInfo.tjsj,
        });
      });
    this.setData({
      createdFormProject: false,
      GocreatedShow: true,
    });
  },
  // 是否前往新项目
  goNewProject() {
    wx.redirectTo({
      url: `/pages/programs/detail/detail?xmbh=${this.data.systemInfo.xmbh}`,
    });
  },
  goOnDown() {
    this.setData({
      createdFormProject: false,
      createdShow: false,
    });
  },

  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      "systemInfo.xmfl": value,
    });
    this.onCancel();
  },
  openPick() {
    this.setData({
      pickerAddShow: true,
    });
  },
  onRest() {
    this.setData({
      systemInfo: {
        ...{}
      },
      createdFormProject: false,
    });
  },
  onCancel() {
    this.setData({
      pickerAddShow: false,
    });
  },
  inputValueSystem(e) {
    const param = e.currentTarget.dataset.name; //字段名
    const value = e.detail; //值
    const key = `systemInfo.${param}`;
    // const msg=`checkData.${param}`;
    this.setData({
      [key]: value,
      // [msg]:'' //清空错误提示
    });
  },
  opencreatedProject() {
    this.setData({
      createdShow: true,
    });
  },
  gocreated() {
    let timestamp = Date.parse(new Date());
    let date = new Date(timestamp);
    //获取年份
    let Y = date.getFullYear();
    //获取月份
    let M =
      date.getMonth() + 1 < 10 ?
      "0" + (date.getMonth() + 1) :
      date.getMonth() + 1;

    let rand = "";
    for (let i = 0; i < 3; i++) {
      let r = Math.floor(Math.random() * 10);
      rand += r;
    }
    let Data = {
      bs: 3,
      jgdmh: this.data.infor.jdgmh ? this.data.infor.jdgmh : "",
      xmfl: "载体项目", //项目类别
      bm: this.data.Info.bm, //部门
      tname: this.data.Info.jname, //报备人员
      rybh: this.data.Info.rybh, //人员编号
      xmbh: `xmbh${Y}${M}${rand}`, //项目编号
      xmmc: "", //项目名称
      xmsq: "", //项目诉求
      //  xmly: "", //项目来源
      bID: this.data.Info.id, //报备人id
      bbsj: "", //报备时间，不显示字段，提交表单时自动生成当前时间YYY-MM-DD hh:mm:ss
      tjsj: "",
      xmztsja: "",
      bsa: this.data.Info.bsa,
    };
    this.setData({
      createdFormProject: true,
      systemInfo: Data,
    });
  },
  onclosecreated() {
    this.setData({
      createdShow: false,
    });
  },
  topChange: function (t) {
    var a = t.currentTarget.dataset.index;
    // 0 == a
    //   ? this.setData({
    //       investment: 0,
    //     })
    //   : 1 == a
    //   ? this.setData({
    //       investment: 1,
    //     })
    //   : 2 == a &&
    //     this.setData({
    //       investment: 2,
    //     });
    this.setData({
      investment: a,
    });
  },
  fangqiyuany: function (t) {
    this.setData({
      inputValue: t.detail.value,
    });
  },
  confirm: function (a) {
    var e = this;
    if (this.data.inputValue) {
      var n = wx.getStorageSync("user");

      t
        .request({
          url: "https://www.qlsyyun.com:3000/abandon",
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          method: "POST",
          data: {
            fqyy: e.data.inputValue,
            bID: e.data.bID,
          },
        })
        .then(function (a) {
          t
            .request({
              url: "https://www.qlsyyun.com:3000/abandon/stjd",
              header: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
              method: "POST",
              data: {
                khbh: e.data.infor.khbh,
                bm: n[1],
                qymc: e.data.infor.qymc,
                bID: e.data.bID,
                zsy: n[0],
                jd: "招商",
                khzt: "招商放弃",
                tname: n[0],
                cid: e.data.bID,
              },
            })
            .then(function (t) {}),
            t
            .request({
              url: "https://www.qlsyyun.com:3000/abandon/zsgj",
              header: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
              method: "POST",
              data: {
                gjnr: e.data.inputValue,
                lxr: e.data.infor.lxr,
                qymc: e.data.infor.qymc,
                bid: e.data.bID,
                tname: n[0],
                bm: n[1],
                khbh: e.data.infor.khbh,
              },
            })
            .then(function (t) {}),
            t
            .request({
              url: "https://www.qlsyyun.com:3000/abandon/stmx",
              header: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
              method: "POST",
              data: {
                qymc: e.data.infor.qymc,
                bid: e.data.bID,
                tname: n[0],
                tjsj: e.data.nowtime,
                khbh: e.data.infor.khbh,
              },
            })
            .then(function (t) {}),
            wx.showToast({
              title: "已放弃",
              icon: "none",
              duration: 1e3,
              mask: !0,
            }),
            wx.navigateBack({
              delta: 1,
            });
        }),
        this.setData({
          hiddenmodalput: !0,
        });
    } else
      wx.showToast({
        title: "请输入放弃原因",
        icon: "none",
        duration: 1e3,
        mask: !0,
      });
  },
  track: function () {
    wx.navigateTo({
      url: "../application/application?bid=" + this.data.bID + "&name=迁址办证",
    });
  },
  onLoad: function (a) {
    var e = this;
    a.id ?
      e.setData({
        mylink: 1,
      }) :
      e.setData({
        mylink: 2,
      });

    
    var n = new Date().getTime(),
      o = new Date(parseInt(n))
      .toLocaleString()
      .replace(/年|月|日/g, "-")
      .slice(0, 10)
      .replace("上", "")
      .replace("下", "")
      .replace(/\//g, "-"),
      s = wx.getStorageSync("user");

    e.setData({
        bID: a.bID,
        bumen: s,
        nowtime: o,
      }),
      t
      .request({
        url: "https://www.qlsyyun.com:3000/information",
        data: {
          bID: e.data.bID,
        },
      })
      .then(function (a) {
        var n = a.data[0].khbh;
        intention(1).then((res) => {
          res.data.forEach((item) => {
            item.text = item.nr;
            item.id = item.na;
          });
          // e.setData({
          //   khyxdj: res.data,
          // });
          res.data.forEach((item) => {
            if (item.id == a.data[0].khyxdj) {
              console.log(item.id);
              a.data[0].khyxdj2 = item.text;
              console.log(a.data[0].khyxdj2);
            }
          });
          console.log(a.data[0].khyxdj2);
          e.setData({
              infor: a.data[0],
              khbh: n,
              qymc: a.data[0].qymc,
            })
        });
     
          console.log("infor", e.data.infor);

        // e.setData({

        // });
        wx.setStorageSync("infor", a.data[0]),
          wx.setStorageSync("qymclxr", e.data.qymc),
          // t
          // .request({
          //   url: "https://www.qlsyyun.com:3000/linkmans",
          //   data: {
          //     khbh: n,
          //   },
          // })
          linkmans(n).then(function (t) {
            t.data.forEach(item=>{
              item.BooleanLxr=false
            })
            e.setData({
              linkman: t.data,
            });
            getsjxq(n).then((res) => {
              e.setData({
                xqList: res.data,
              });
            });
          });

        var o = wx.getStorageSync("user");
        t.request({
          url: "https://www.qlsyyun.com:3000/attention/all",
        }).then(function (t) {
          t.data.length > 0 &&
            t.data.find(function (t) {
              return t.qymc === e.data.infor.qymc && t.gzr === o[0];
            }) &&
            e.setData({
              collected: !1,
            });
        });
      }),
      t
    // .request({
    //   url: "https://www.qlsyyun.com:3000/record/follow",
    //   data: {
    //     bid: a.bID,
    //   },
    // })
    getFllowDetail(a.bID,n).then(function (t) {
        console.log(t)
        t.data[0].formatTime = e.formatTime(t.data[0].gjsj, 2)
        e.setData({

            follow: t.data[0],
          }),
          null == e.data.follow &&
          e.setData({
            zanwu: !1,
          });
      }),
      t
      .request({
        url: "https://www.qlsyyun.com:3000/sslb",
      })
      .then(function (t) {
        e.setData({
          nr: t.data[0].nr,
        });
      });
  },

  preventTouchMove: function (t) {},
  renewal: function (e) {
    var t = this.data.bumen[1];
    (this.data.bumen[1] = "科澜") && (t = "科澜项目"),
    (6 == this.data.bumen[4] && t == this.data.infor.bm) ||
    this.data.bumen[0] == this.data.infor.zsy ||
      9 == this.data.bumen[4] ?
      wx.navigateTo({
        url: `../renew/renew?type=${e.currentTarget.dataset.type}`,
      }) :
      wx.showToast({
        title: "仅操作本部门",
        icon: "none",
      });
  },
  showPopup: function () {
    this.setData({
      show: !0,
    });
  },
  onClose: function () {
    this.setData({
      show: !1,
    });
  },
  onShow: function () {
    var a = this;

    0 == a.data.a &&
      wx.showToast({
        title: "操作成功",
        icon: "none",
        duration: 1e3,
      }),
      // t
      // .request({
      //   url: "https://www.qlsyyun.com:3000/linkmans",
      //   data: {
      //     khbh: a.data.khbh,
      //   },
      // })
     
      t.request({
        url: "https://www.qlsyyun.com:3000/enterprise",
        data: {
          bID: a.data.bID,
        },
      })
      .then(function (t) {
        var e = t.data;
        a.setData({
            emphasis: e[0].khfj,
          }),
          "重点客户" == a.data.emphasis ?
          a.setData({
            collecteds: !1,
          }) :
          a.setData({
            collecteds: !0,
          });
      }),
      // t
    // .request({
    //   url: "https://www.qlsyyun.com:3000/record/follow",
    //   data: {
    //     bid: a.data.bID,
    //   },
    // })
    console.log(2221,a.data.khbh)
    if(a.data.khbh){
      linkmans(a.data.khbh).then(function (t) {
        t.data.forEach(item=>{
          item.BooleanLxr=false
        })
        a.setData({
          linkman: t.data,
        });
      })
    }
    a.setData({
      show: !1,
    }),
    getFllowDetail(a.data.bID,a.data.khbh).then(function (t) {
      t.data[0].formatTime = a.formatTime(t.data[0].gjsj, 2)

      a.setData({
          follow: t.data[0],
        }),
        t.data.length > 0 &&
        a.setData({
          zanwu: !0,
        });
    });
  },
  onCollectionTap: function () {
    this.setData({
      collected: !this.data.collected,
    });
    var a = wx.getStorageSync("user");
    0 == this.data.collected ?
      t
      .request({
        url: "https://www.qlsyyun.com:3000/allocation/zscbgzlb",
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        method: "POST",
        data: {
          bID: this.data.bID,
          khbh: this.data.infor.khbh,
          qymc: this.data.infor.qymc,
          bm: a[1],
          zsy: a[0],
        },
      })
      .then(function (t) {
        wx.showToast({
          title: "关注成功",
          icon: "none",
          duration: 1e3,
        });
      }) :
      t
      .request({
        url: "https://www.qlsyyun.com:3000/allocation/unzscbgzlb",
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        method: "POST",
        data: {
          bID: this.data.bID,
          qymc: this.data.infor.qymc,
          zsy: a[0],
        },
      })
      .then(function (t) {
        wx.showToast({
          title: "取消关注成功",
          icon: "none",
          duration: 1e3,
        });
      });
  },
  formatTime(Time, type = 1) {
    // console.log(Time)
    let date = new Date(Time)
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    const second = date.getSeconds().toString().padStart(2, '0');
    if (type == 1) {
      return `${year}-${month}-${day}`;

    } else {
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    }

  },
  quxiao: function () {
    var a = this;
    a.data.bumen[4] > 6 ?
      t
      .request({
        url: "https://www.qlsyyun.com:3000/enter",
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        method: "POST",
        data: {
          bID: a.data.bID,
          khfj: "一般客户",
        },
      })
      .then(function (t) {
        wx.showToast({
            title: "取消重点成功",
            icon: "none",
            duration: 1e3,
          }),
          a.setData({
            collecteds: !0,
          });
      }) :
      wx.showToast({
        title: "暂无权限",
        icon: "none",
        duration: 1e3,
      });
  },
  onCollectionTaps: function () {
    var a = this;
    // 测试工作通知专用（勿删）
    // t.request({
    //   url: "http://localhost:8001/enterprise/sxtx",
    //   header: {
    //     "Content-Type": "application/x-www-form-urlencoded"
    //   },
    //   method: "POST",
    //   data: {
    //     qymc: '测试企业',
    //     khbh: 'kh123456789',
    //     tnamea: '系统更新',
    //     bm: '综合管理中心',
    //     yxxm: '测试自动提示项目',
    //     zsfx: '自动发送通知已完成 但不能在同一时间多人发送同一通知',
    //     lrid: a.data.bID,
    //     tnameb: '曾慧钦'
    //   }
    // }).then((function (t) {}))
    // return

    a.data.bumen[4] > 5 &&
      t
      .request({
        url: "https://www.qlsyyun.com:3000/enter",
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        method: "POST",
        data: {
          bID: a.data.bID,
          khfj: "重点客户",
        },
      })
      .then(function (e) {
        wx.showToast({
            title: "设置重点成功",
            icon: "none",
            duration: 1e3,
          }),
          a.setData({
            collecteds: !1,
          }),
          t
          .request({
            url: "https://www.qlsyyun.com:3000/csjl",
            data: {
              bID: a.data.bID,
            },
          })
          .then(function (e) {
            a.setData({
                jilu: e.data[0],
              }),
              t
              .request({
                url: "https://www.qlsyyun.com:3000/enterprise/sxtx",
                header: {
                  "Content-Type": "application/x-www-form-urlencoded",
                },
                method: "POST",
                data: {
                  qymc: e.data[0].qymc,
                  khbh: e.data[0].khbh,
                  // tnamea: a.data.bumen[0],
                  tnamea: "系统更新",
                  bm: e.data[0].bm,
                  yxxm: e.data[0].yxxm,
                  zsfx: e.data[0].zsfx,
                  lrid: a.data.bID,
                  // tnameb: a.data.nr,
                  tnameb: "曾慧钦",
                },
              })
              .then(function (t) {});
          });
      });
  },
});